﻿using System;
using Microsoft.Phone.Controls;
using XBMCRemote.DataModel;
using XBMCRemote.DataModel.Entities;
using XBMCRemote.UseCase;

namespace XBMCRemote
{
    public partial class SettingsPage : PhoneApplicationPage
    {
        ConfigurationUseCase _configurationUseCase;
        private Configuration _selectedConfiguration;
        private Guid _selectedConfigurationId = Guid.Empty;

        public SettingsPage()
        {
            InitializeComponent();
            _configurationUseCase = (App.Current as App).ConfigurationUseCase;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string configurationId;

            if (NavigationContext.QueryString.TryGetValue("id", out configurationId))
            {
                _selectedConfigurationId = new Guid(configurationId);
                _selectedConfiguration = _configurationUseCase.GetConfigurationById(_selectedConfigurationId);

                if (_selectedConfiguration != null)
                {
                    NameTextBox.Text = _selectedConfiguration.Name;
                    HostTextBox.Text = _selectedConfiguration.Host;
                    PortTextBox.Text = _selectedConfiguration.Port;
                    UsernameTextBox.Text = _selectedConfiguration.Username;
                    PasswordTextBox.Text = _selectedConfiguration.Password;
                }
            }
        }

        private void AppBarBtnSave_Click(object sender, System.EventArgs e)
        {
			// Save the connection settings and go back to the connections page.
            Configuration configuration = new Configuration();
            configuration.Name = NameTextBox.Text;
            configuration.Host = HostTextBox.Text;
            configuration.Port = PortTextBox.Text;
            configuration.Username = UsernameTextBox.Text;
            configuration.Password = PasswordTextBox.Text;

            if (_selectedConfigurationId == Guid.Empty)
            {
                configuration.Id = Guid.NewGuid();
                configuration.IsSelected = true;
                _configurationUseCase.AddNewConfiguration(configuration);
            }
            else
            {
                configuration.Id = _selectedConfiguration.Id;
                configuration.IsSelected = true;
                _configurationUseCase.RemoveCurrentSelectedConfiguration();
                _configurationUseCase.UpdateConfiguration(configuration);
            }
            (App.Current as App).XbmcHelper = new XbmcHelper(configuration);
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void AppBarBtnCancel_Click(object sender, System.EventArgs e)
        {
			// Go back to the homepage without saving.
            NavigationService.Navigate(new Uri("/Pages/ConnectionsPage.xaml", UriKind.Relative));
        }
    }
}
